package cn.javaxc.mvcwork.framework.actions;
import cn.javaxc.mvcwork.framework.beans.TFormBean;
import cn.javaxc.mvcwork.util.helper.RequestHelper;
import cn.javaxc.mvcwork.controller.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.PageContext;
/**
 * TActionContext 是框架的Action上下文<br/>
 * 保存了每次Action处理的HttpServletRequest与HttpServletResponse对象<br/>
 * 通过ActionContext对象可以获得在上下文中的各种对象<br/>
 * 
 * @mvcversion 1.0
 *
 */

public class TActionContext{
  private HttpServletRequest request = null;
  private HttpServletResponse response = null;
  private HttpSession session = null;
  private Servlet servlet = null;
  private Object action = null;
  private Object bean = null;
  public final static String MESSAGE = "MESSAGE";
  public final static String ERROR = "actionError";
  private TActionForward forward;
  protected PageContext pageContext;

  
  public TActionContext(Servlet servlet, Object bean, Object action, HttpServletRequest request, HttpServletResponse response) {
    this.bean = bean;
    this.request = request;
    this.response = response;
    this.servlet = servlet;
    this.action = action;
  }


  
  public TActionContext(Servlet servlet, HttpServletRequest request, HttpServletResponse response) {
    this.request = request;
    this.response = response;
    this.servlet = servlet;
  }
  public void setMessage(String value) {
    // if the bean object is an instance of the TFormBean class, set the message
    // on the bean, as well as a request attribute
    if (bean instanceof TFormBean) {
      ((TFormBean) bean).setMessage(value);
    }
    getRequest().setAttribute(MESSAGE, value);
  }


  
  public ServletContext getServletContext() {
    return servlet.getServletConfig().getServletContext();
  }


  
  public Servlet getServlet() {
    return servlet;
  }

  public Object getBean() {
    return bean;
  }
	public void setBean(Object bean){
		this.bean=bean;
	}
  
  public TFormBean getFormBean() {
	if (bean instanceof TFormBean) {
      return (TFormBean) bean;
    }
    return null;
  }





  public Object getAction() {
    return action;
  }


  
  public String getBrowser() {
    return getRequest().getHeader("USER-AGENT");
  }


  


	public void setAttribute(String name, Object value){
		getRequest().setAttribute(name,value);
	}
	public Object getAttribute(String name){
		return getRequest().getAttribute(name);
	}
  
  public String getParameter(String paramName) {
    return getRequest().getParameter(paramName);
  }
  public String getParameter(String paramName,String def) {
	if (getRequest().getParameter(paramName)!=null){
		return getRequest().getParameter(paramName);
	}
	return def;
  }

  
  public String getIpAddress() {
    return getRequest().getRemoteAddr();
  }


  
  public HttpServletRequest getRequest() {
    return request;
  }


  
  public HttpServletResponse getResponse() {
    return response;
  }


  
  public HttpSession getSession() {
    return session;
  }

  public PageContext getPageContext(){
	return this.pageContext;
  }
  public void setPageContext(PageContext pageContext){
	this.pageContext=pageContext;
  }
	public TActionForward getForward(){
		return this.forward;
	}
	public void setForward(TActionForward forward){
		this.forward=forward;
	}
	public void setActionError(String err){
		request.setAttribute("actionError", err);
	}
	public String getActionError(){
		return (String)request.getAttribute("actionError");
	}
	public void setError(Throwable err){
		request.setAttribute("Error", err);
	}
	public Throwable getError(){
		return (Throwable)request.getAttribute("Error");
	}
	public void setActionInfo(String err){
		request.setAttribute("actionInfo", err);
	}
	public String getActionInfo(){
		return (String)request.getAttribute("actionInfo");
	}
	public  MVCConfig getConfig(){
		return MVCConfig.getConfig();
	}
}
